package com.xb.loan.cashier.biz.pay.bankcomm;

import com.xb.loan.cashier.biz.pay.PaymentForCharge;
import com.xb.loan.cashier.biz.pay.bankcomm.util.BankcommCore;
import com.xb.loan.cashier.biz.pay.bankcomm.util.BankcommSubmit;
import com.xb.loan.cashier.biz.pay.bean.PaymentChargeOrder;
import com.xb.loan.util.DateHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class BankcommPaymentForCharge extends PaymentForCharge {
	
	private final static Logger logger = LoggerFactory.getLogger(BankcommPaymentForCharge.class);
	
	private final static String ORDER_DATE_FMT = "yyyyMMdd";
	private final static String ORDER_TIME_FMT = "HHmmss";
	private DecimalFormat df = new DecimalFormat("#0.00");
	
	@Override
	public String chargeByApp(PaymentChargeOrder paymentChargeOrder)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String chargeByWap(PaymentChargeOrder paymentChargeOrder)
			throws Exception {
 		String merID = paymentChargeOrder.getChannelUser(); //商户号码
		String orderId = paymentChargeOrder.getOrderId();   //商户订单号
		String orderDate = DateHelper.formatDate(ORDER_DATE_FMT, new Date());// 格式为YYYYMMdd
		String orderTime = DateHelper.formatDate(ORDER_TIME_FMT, new Date());  // 格式为HHmmss
		double amount = paymentChargeOrder.getMoney();         // 15位整数+2位小数 单位元 交易金额 
		String orderContent = paymentChargeOrder.getTitle();  // 100位 订单内容
		String orderMono = paymentChargeOrder.getDetail();      // 100位 商户备注
		String merURL = paymentChargeOrder.getNotifyUrl();     //后台通知地址
		String goodsURL = paymentChargeOrder.getCallBackUrl(); //前台通知地址
		String mobileURL = paymentChargeOrder.getPayUrlBatch(); //mobileURL
		String apiURL = paymentChargeOrder.getQueryUrl();//apiURL
		String period = "";//格式为YYYYMMddHHmmss 手机支付交易超时时间
		String payBatchNo = "";    //10位 商家可填入自己的批次号，对账使用
		String sndMerName= "";   //30位 二级商户名称
		String sndMerType= "";   //4位 二级商户类型
		String sndMerNo= "";    //20位 二级商户编号
		
		String html = null;
				
		//不要改变参数顺序
		Map<String, LinkedHashMap<String, Object>> params = new HashMap<String, LinkedHashMap<String, Object>>();
		LinkedHashMap<String, Object> headMap = new LinkedHashMap<String, Object>();
		headMap.put("TranCode", BankcommConst.TRAN_CODE_MOBILE_PAY);
		headMap.put("MerchNo", merID);
		LinkedHashMap<String, Object> bodyMap = new LinkedHashMap<String, Object>();
		bodyMap.put("InterfaceVersion", BankcommConst.INTERFACE_VERSION);
		bodyMap.put("OrderNo", orderId);
		bodyMap.put("OrderDate", orderDate);
		bodyMap.put("OrderTime", orderTime);
		bodyMap.put("TranType", BankcommConst.TRAN_TYPE);
		bodyMap.put("Amount", df.format(amount));
		bodyMap.put("CurType", BankcommConst.CUR_TYPE);
		bodyMap.put("OrderContent", orderContent);
		bodyMap.put("OrderMono", orderMono);
		bodyMap.put("Period", period);
		bodyMap.put("PhdFlag", "");
		bodyMap.put("NotifyType", BankcommConst.NOTIFY_TYPE);
		bodyMap.put("MerURL", merURL);
		bodyMap.put("GoodsURL", goodsURL);
		bodyMap.put("JumpSeconds", "");
		bodyMap.put("PayBatchNo", payBatchNo);
		bodyMap.put("SndMerName", sndMerName);
		bodyMap.put("SndMerType", sndMerType);
		bodyMap.put("SndMerNo", sndMerNo);
		bodyMap.put("NetType", BankcommConst.NET_TYPE);
		params.put(BankcommConst.HEAD, headMap);
		params.put(BankcommConst.BODY, bodyMap);
		Map<String, Object> xmlResMap = BankcommPayment.apiRequest(apiURL, params);//ApiURL
		if (xmlResMap==null) {
			logger.info("chargeByWap 创建订单返回结果为空！");
			return html;
		}
		Map<String, String> headResMap = (Map<String, String>)xmlResMap.get(BankcommConst.HEAD);
		String rspType = headResMap.get(BankcommConst.RSP_TYPE);
		if (BankcommConst.SUCC.equals(rspType)) {//RspType为响应状态 N为正常 E为错误
			html = BankcommPayment.mobilePaySubForm(merID, orderId, orderDate, mobileURL);
		}else {
			String rspCode = headResMap.get(BankcommConst.RSP_CODE);
			String rspMsg = headResMap.get(BankcommConst.RSP_MSG);
			logger.info("chargeByWap 提交订单异常   rspType="+rspType+",rspCode="+rspCode+",rspMsg="+rspMsg);
			return html;
		}
		return html;
	}

	@Override
	public String chargeByWeb(PaymentChargeOrder paymentChargeOrder)
			throws Exception {
		
		String merID = paymentChargeOrder.getChannelUser(); //商户号码
		String orderId = paymentChargeOrder.getOrderId();   //商户订单号
		String orderDate = DateHelper.formatDate("yyyyMMdd", new Date());// 格式为YYYYMMdd
		String orderTime = DateHelper.formatDate("HHmmss", new Date());  // 格式为HHmmss
		double amount = paymentChargeOrder.getMoney();         // 15位整数+2位小数 单位元 交易金额 
		String orderContent = paymentChargeOrder.getTitle();  // 100位 订单内容
		String orderMono = paymentChargeOrder.getDetail();	   // 100位 商户备注
		String merURL = paymentChargeOrder.getNotifyUrl();     //后台通知地址
		String goodsURL = paymentChargeOrder.getCallBackUrl(); //前台通知地址
		String postURL = paymentChargeOrder.getPayUrl(); //支付地址
		String payBatchNo="";             //10位 商家可填入自己的批次号，对账使用
		String proxyMerName="";           //30位 二级商户名称
		String proxyMerType="";               //4位 二级商户类型
		String proxyMerCredentials="";    //20位 二级商户编号
		String orderTimeout="";     //格式为YYYYMMddHHmmss B2C交易超时时间
		String extInfo="";
		
		//不要改变参数顺序
		LinkedHashMap<String, String> requestData = new LinkedHashMap<String, String>();
		requestData.put("interfaceVersion", BankcommConst.INTERFACE_VERSION); //版本号
		requestData.put("merID", merID);//商户号
		requestData.put("orderid", orderId);//订单号
		requestData.put("orderDate", orderDate);//商户订单日期
		requestData.put("orderTime", orderTime);//商户订单时间
		requestData.put("tranType", BankcommConst.TRAN_TYPE);//提交类型 0为B2C支付交易
		requestData.put("amount", df.format(amount));//交易金额
		requestData.put("curType", BankcommConst.CUR_TYPE);//交易币种
		requestData.put("orderContent", orderContent);//订单内容
		requestData.put("orderMono", orderMono);//商户备注
		requestData.put("phdFlag", BankcommConst.PHD_FLAG);
		requestData.put("notifyType", BankcommConst.NOTIFY_TYPE);
		requestData.put("merURL", merURL);//后台通知URL
		requestData.put("goodsURL", goodsURL);//跳转通知URL
		requestData.put("jumpSeconds", BankcommConst.JUMP_SECONDS);
		requestData.put("payBatchNo", payBatchNo);//商户批次号
		requestData.put("proxyMerName", proxyMerName);//二级商户名称
		requestData.put("proxyMerType", proxyMerType);//二级商户类型
		requestData.put("proxyMerCredentials", proxyMerCredentials);//二级商户编号
		requestData.put("netType", BankcommConst.NET_TYPE);
		requestData.put("orderTimeout", orderTimeout);//超时时间
		requestData.put("extInfo", extInfo);//二级商户扩展字段
		Map<String, String> submitFromData = BankcommCore.detachedSign(requestData);
		String html = BankcommSubmit.createAutoFormHtml(postURL, submitFromData,BankcommConst.ENCODING_GBK);   //生成自动跳转的Html表单
		return html;
	}
	public static void main(String[] args) {
		System.out.println(new DecimalFormat("#0.00").format(1));
		System.out.println(DateHelper.formatDate("yyyyMMdd", new Date()));
	}

}
